//
//  DateHelper.m
//  HealthManager_Customer
//
//  Created by lzh on 2018/1/4.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "DateHelper.h"

@implementation DateHelper

+ (NSString*)dateStrYMDFromTimeStamp:(NSString*)timeStamp
{
    if (timeStamp.length == 0) {
        return @"";
    }
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue] / 1000.0];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}

+ (NSString*)dateStrFromTimeStamp:(NSString*)timeStamp
{
    if (timeStamp.length == 0) {
        return @"";
    }
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue] / 1000.0];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}

+ (NSString*)dateStrFromTimeStamp:(NSString*)timeStamp dateFormat:(NSString *)dateformatStr
{
    if (timeStamp.length == 0) {
        return @"";
    }
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue] / 1000.0];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateformatStr];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}

+ (NSString*)m_dateStrFromTimeStamp:(NSString*)timeStamp
{
    if (timeStamp.length == 0) {
        return @"";
    }
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue] / 1000.0];
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd HH:mm:ss"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}

+ (NSDate *)stringDateToDate:(NSString *)dateStr{
    
    NSDate *resultDate;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
    formatter.timeZone = sourceTimeZone;
    resultDate = [formatter dateFromString:[NSString stringWithFormat:@"%@",dateStr]];
    return resultDate;
}

+ (NSString *)stringDateToLongTimeStamp:(NSString *)dateStr {
    
    NSString *resultLongTimeStamp = @"";
    NSDate *date = [self stringDateToDate:dateStr];
    long long dateTimeStamp = [date timeIntervalSince1970] *1000;
    resultLongTimeStamp = [NSString stringWithFormat:@"%lld",dateTimeStamp];
    return resultLongTimeStamp;
}

+ (NSString *)stringDateToLongTimeStamp:(NSString *)dateStr dateFormat:(NSString *)dateformat {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:dateformat];
    NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
    formatter.timeZone = sourceTimeZone;
    NSDate *resultDate = [formatter dateFromString:[NSString stringWithFormat:@"%@",dateStr]];
    
    long long dateTimeStamp = [resultDate timeIntervalSince1970] *1000;
    NSString *resultLongTimeStamp = [NSString stringWithFormat:@"%lld",dateTimeStamp];
    
    return resultLongTimeStamp;
}
+ (NSComparisonResult)compareTime:(NSString *)startTime withEndTime:(NSString *)endTime dateFormat:(NSString *)dateformat
{

    NSInteger startNum = [[self stringDateToLongTimeStamp:startTime dateFormat:dateformat] integerValue];
    
    
     NSInteger endNum = [[self stringDateToLongTimeStamp:endTime dateFormat:dateformat] integerValue];
    
    NSInteger num = startNum - endNum;
    
    if (num > 0) {
        return NSOrderedAscending;
    }else if (num == 0){
        return NSOrderedSame;
    }else{
        return NSOrderedDescending;
    }
}
// 当前时间
+ (NSString*)curTimeStr{
    
    NSTimeInterval curTimeStamp = [[NSDate date] timeIntervalSince1970];
    
    return [NSString stringWithFormat:@"%lld",@(curTimeStamp * 1000).longLongValue];
}

+(NSString*) compareCurrentTime:(NSString*) dateStr
{
    NSDate* compareDate = [[NSDate alloc]initWithTimeIntervalSince1970:dateStr.longLongValue / 1000];
    
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    
    timeInterval = -timeInterval;
    
    long temp = 0;
    
    NSString *result;
    
    if (timeInterval < 60) {
        
        result = [NSString stringWithFormat:@"刚刚"];
        
    }
    
    else if((temp = timeInterval/60) <60){
        
        result = [NSString stringWithFormat:@"%d分前",temp];
        
    }
    
    
    
    else if((temp = temp/60) <24){
        
        result = [NSString stringWithFormat:@"%d小时前",temp];
        
    }
    
    
    
    else if((temp = temp/24) <30){
        
        result = [NSString stringWithFormat:@"%d天前",temp];
        
    }
    
    
    
    else if((temp = temp/30) <12){
        
        result = [NSString stringWithFormat:@"%d月前",temp];
        
    }
    
    else{
        
        temp = temp/12;
        
        result = [NSString stringWithFormat:@"%d年前",temp];
        
    }
    
    
    
    return  result;
    
}

@end
